package com.wang.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.mall.bean.SmsSeckillSession;
import com.wang.mall.bean.SmsSeckillSkuRelation;
import com.wang.mall.mapper.SmsSeckillSessionMapper;
import com.wang.mall.service.SmsSeckillSessionService;
import com.wang.mall.service.SmsSeckillSkuRelationService;
import com.wang.mall.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SmsSeckillSessionServiceImpl extends ServiceImpl<SmsSeckillSessionMapper, SmsSeckillSession> implements SmsSeckillSessionService {

    @Autowired
    SmsSeckillSkuRelationService smsSeckillSkuRelationService;

    @Override
    public PageUtils queryPage(Page<SmsSeckillSession> page, String startTime, String endTime, String createTime) {

        if (StringUtils.isEmpty(startTime)) {
            LocalDateTime min = LocalDateTime.MIN;
            startTime = min.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).substring(1);
        }

        if (StringUtils.isEmpty(endTime)) {
            LocalDateTime max = LocalDateTime.MAX;
            endTime = max.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).substring(1);
        }


        IPage<SmsSeckillSession> page1 = page(page, new LambdaQueryWrapper<SmsSeckillSession>()
                .like(!StringUtils.isEmpty(createTime), SmsSeckillSession::getCreateTime, createTime)
                .ge(!StringUtils.isEmpty(startTime), SmsSeckillSession::getStartTime, startTime)
                .le(!StringUtils.isEmpty(endTime), SmsSeckillSession::getEndTime, endTime));

        return new PageUtils(page1);


    }

    @Override
    public List<SmsSeckillSession> uploadSkillProduct() {
        // 计算最近三天的时间

        LocalDate now = LocalDate.now();

        String startTime = now.atTime(LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endTime = now.plusDays(2).atTime(LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        // 查询从现在开始 三天之内的秒杀活动
        List<SmsSeckillSession> list = list(new LambdaQueryWrapper<SmsSeckillSession>()
                .between(SmsSeckillSession::getStartTime, startTime, endTime));

        if (!CollectionUtils.isEmpty(list)) {
            // 查询活动商品
            return list.stream().map(smsSeckillSession -> {
                List<SmsSeckillSkuRelation> relationList = smsSeckillSkuRelationService.list(new LambdaQueryWrapper<SmsSeckillSkuRelation>().eq(SmsSeckillSkuRelation::getPromotionSessionId, smsSeckillSession.getId()));
                smsSeckillSession.setList(relationList);
                return smsSeckillSession;
            }).collect(Collectors.toList());
        }

        return list;
    }


    public static void main(String[] args) {
        LocalDate now = LocalDate.now();

        String startTime = now.atTime(LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endTime = now.plusDays(2).atTime(LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("endTime = " + endTime);
        System.out.println("startTime = " + startTime);

    }
}
